export default class player_rabbit extends Laya.Script {
    
    constructor() { 
        super();
        player_rabbit._intance = this
     }
    private player_ani_1:Laya.Animation        //白兔子动画
    private player_ani_2:Laya.Animation        //黑兔子动画
    private box_collider_1:Laya.BoxCollider    //白兔子盒子碰撞器
    private rigid_body_1:Laya.RigidBody        //白兔子刚体组件
    private box_collider_2:Laya.BoxCollider    //黑兔子盒子碰撞器
    private rigid_body_2:Laya.RigidBody        //黑兔子刚体组件

    private isleft:boolean                     //是否朝左
    private isdown:boolean                     //是否按下
    private isgameover:boolean                 //是否游戏结束

    // private set_up:number
    // private set_left:number
    // private set_right:number

    public static _intance: player_rabbit

    onEnable(): void {
        this.initPlayer(this.owner)
        this.rigidPlayer()
        this.isleft = false
        this.isgameover = false
    }

    public initPlayer(node:Laya.Node){
        //白兔子
        this.player_ani_1=new Laya.Animation()
        //黑兔子
        this.player_ani_2=new Laya.Animation()
        
        //加载白兔子图集动画
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/idelLeft.atlas",null,'idelleft')//闲置状态动画
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/idelRight.atlas",null,'idelright')
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/runLeft.atlas",null,'runleft')//奔跑状态动画
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/runRight.atlas",null,'runright')
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/jumpLeft.atlas",null,'jumpleft')//跳跃状态动画
        this.player_ani_1.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/jumpRight.atlas",null,'jumpright')
        //加载黑兔子图集动画
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/idelLeft_1.atlas",null,'idelleft_1')//闲置状态动画
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/idelRight_1.atlas",null,'idelright_1')
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/runLeft_1.atlas",null,'runleft_1')//奔跑状态动画
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/runRight_1.atlas",null,'runright_1')
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/jumpLeft_1.atlas",null,'jumpleft_1')//跳跃状态动画
        this.player_ani_2.loadAtlas("F:/MyLaya/Rabbit_Gohome/laya/assets/ani/jumpRight_1.atlas",null,'jumpright_1')

        //将白兔子添加到舞台
        this.owner.addChild(this.player_ani_1)
        // this.player_ani.pos(x,y)
        //白兔子动画播放间隔
        this.player_ani_1.interval=200
        //白兔子设置缩放比例
        this.player_ani_1.scale(1.1,1.1)
        //白兔子设置位置
        this.player_ani_1.pos(40,230)
        this.player_ani_1.play(0,true,'idelright')

        //将黑兔子添加到舞台
        this.owner.addChild(this.player_ani_2)
        // this.player_ani.pos(x,y)
        //黑兔子动画播放间隔
        this.player_ani_2.interval=200
        //黑兔子设置缩放比例
        this.player_ani_2.scale(0.97,0.97)
        //黑兔子设置位置
        this.player_ani_2.pos(2,227)
        this.player_ani_2.play(0,true,'idelright_1')
    }

    rigidPlayer(){
        //白兔添加Boxcollider组件
        this.box_collider_1=this.player_ani_1.addComponent(Laya.BoxCollider)
        this.box_collider_1.width = 50
        this.box_collider_1.height = 40
        this.box_collider_1.y=15
        this.box_collider_1.label = "player1"
        //白兔添加RigidBody组件
        this.rigid_body_1=this.player_ani_1.addComponent(Laya.RigidBody)
        //白兔设置重力系数
        this.rigid_body_1.gravityScale=10
        this.rigid_body_1.allowRotation = false
        this.rigid_body_1.allowSleep = false
        this.rigid_body_1.bullet = true

        //黑兔添加Boxcollider组件
        this.box_collider_2=this.player_ani_2.addComponent(Laya.BoxCollider)
        this.box_collider_2.width = 50
        this.box_collider_2.height = 50
        this.box_collider_2.y=15
        this.box_collider_2.label = "player2"
        //黑兔添加RigidBody组件
        this.rigid_body_2=this.player_ani_2.addComponent(Laya.RigidBody)
        //黑兔设置重力系数
        this.rigid_body_2.gravityScale=10
        this.rigid_body_2.allowRotation = false
        this.rigid_body_2.allowSleep = false
        this.rigid_body_2.bullet = true
    }



    /**按键按下 */
    onKeyDown(e:any){
        if(this.isgameover == false){
        this.isdown = true
        //白兔
        //左键
        if(e.keyCode == 37){
            this.player_ani_1.play(0,true,'runleft')
            Laya.Tween.to(
            this.player_ani_1,
            {x:this.player_ani_1.x - 60},
            120,
            null
            )   
            this.isleft = true
        }
        //右键
        if(e.keyCode == 39){
            this.player_ani_1.play(0,true,'runright')
            Laya.Tween.to(
                this.player_ani_1,
                {x:this.player_ani_1.x + 60},
                120,
                null
            )  
            this.isleft = false
        }
        //上跳
        if(e.keyCode == 38&&this.isleft == false){
            this.player_ani_1.play(0,true,'jumpright')
            Laya.Tween.to(
                this.player_ani_1,
                {y:this.player_ani_1.y - 70 },
                120,
                null
            )  
        }else if(e.keyCode == 38&&this.isleft == true){
            this.player_ani_1.play(0,true,'jumpleft')
            Laya.Tween.to(
                this.player_ani_1,
                {y:this.player_ani_1.y - 70 },
                120,
                null
            )  
        }
        // //下落
        // if(e.keyCode == 40&&this.isleft == false){
        //     this.player_ani_1.play(0,true,'jumpright')
        //     Laya.Tween.to(
        //         this.player_ani_1,
        //         {y:this.player_ani_1.y + 100},
        //         200,
        //         null
        //     )
        // }else if(e.keyCode == 40&&this.isleft == true){
        //     this.player_ani_1.play(0,true,'jumpleft')
        //     Laya.Tween.to(
        //         this.player_ani_1,
        //         {y:this.player_ani_1.y + 100},
        //         200,
        //         null
        //     )
        // }

        //黑兔
        //左键
        if(e.keyCode == 65){
            this.player_ani_2.play(0,true,'runleft_1')
            Laya.Tween.to(
                this.player_ani_2,
                {x:this.player_ani_2.x - 60},
                120,
                null
            )
            this.isleft = true
        }
        //右键
        if(e.keyCode == 68){
            this.player_ani_2.play(0,true,'runright_1')
            Laya.Tween.to(
                this.player_ani_2,
                {x:this.player_ani_2.x + 60},
                120,
                null
            )
            this.isleft = false
        }
        //上跳
        if(e.keyCode == 87&&this.isleft == false){
            this.player_ani_2.play(0,true,'jumpright_1')
            Laya.Tween.to(
                this.player_ani_2,
                {y:this.player_ani_2.y - 70},
                120,
                null
            )
        }else if(e.keyCode == 87&&this.isleft == true){
            this.player_ani_2.play(0,true,'jumpleft_1')
            Laya.Tween.to(
                this.player_ani_2,
                {y:this.player_ani_2.y - 70},
                120,
                null
            )
        }
        // //下落
        // if(e.keyCode == 83&&this.isleft == false){
        //     this.player_ani_2.play(0,true,'jumpright_1')
        //     Laya.Tween.to(
        //         this.player_ani_2,
        //         {y:this.player_ani_2.y + 100},
        //         200,
        //         null
        //     )
        // }else if(e.keyCode == 83&&this.isleft == true){
        //     this.player_ani_2.play(0,true,'jumpleft_1')
        //     Laya.Tween.to(
        //         this.player_ani_2,
        //         {y:this.player_ani_2.y + 100},
        //         200,
        //         null
        //     )
        // }
    }
}

    /**按键放开 */
    onKeyUp(e:any){
        //白兔
        if(e.keyCode == 37){
            this.player_ani_1.play(0,true,'idelleft')
        }
        if(e.keyCode == 39){
            this.player_ani_1.play(0,true,'idelright')
        }
        if(e.keyCode == 38&&this.isleft == false){
            this.player_ani_1.play(0,true,'idelright')
        }else if(e.keyCode == 38&&this.isleft == true){
            this.player_ani_1.play(0,true,'idelleft')
        }
        if(e.keyCode == 40&&this.isleft == false){
            this.player_ani_1.play(0,true,'idelright')
        }else if(e.keyCode == 40&&this.isleft == true){
            this.player_ani_1.play(0,true,'idelleft')
        }
        //黑兔
        if(e.keyCode == 65){
            this.player_ani_2.play(0,true,'idelleft_1')
        }
        if(e.keyCode == 68){
            this.player_ani_2.play(0,true,'idelright_1')
        }
        if(e.keyCode == 87&&this.isleft == false){
            this.player_ani_2.play(0,true,'idelright_1')
        }else if(e.keyCode == 87&&this.isleft == true){
            this.player_ani_2.play(0,true,'idelleft_1')
        }
        if(e.keyCode == 83&&this.isleft == false){
            this.player_ani_2.play(0,true,'idelright_1')
        }else if(e.keyCode == 83&&this.isleft == true){
            this.player_ani_2.play(0,true,'idelleft_1')
        }
    }

        /**射线检测 */
        // Raycast() {
        //     var get_left = 10
        //     var get_right = 10
        //     var get_up = 20
        //     var get_down = 10
        //     var worldLeft = Laya.Physics.I.world
        //     var worldRight = Laya.Physics.I.world
        //     var worldUp = Laya.Physics.I.world
        //     var worldDown = Laya.Physics.I.world
        //     worldLeft.RayCast(function () {
        //         get_left = 0
        //     }, {
        //             x: this.player_ani_1.x,
        //             y: this.player_ani_1.y
        //         }, {
        //             x: this.player_ani_1.x - 35,
        //             y: this.player_ani_1.y
        //         })
    
        //     worldRight.RayCast(function () {
        //         get_right = 0
        //     }, {
        //             x: this.player_ani_1.x,
        //             y: this.player_ani_1.y
        //         }, {
        //             x: this.player_ani_1.x + 35,
        //             y: this.player_ani_1.y
        //         })
        //     worldUp.RayCast(function () {
        //         get_up = 0
        //     }, {
        //             x: this.player_ani_1.x,
        //             y: this.player_ani_1.y
        //         }, {
        //             x: this.player_ani_1.x,
        //             y: this.player_ani_1.y + 50
        //         })
        //      this.set_left = get_left
        //      this.set_right = get_right
        //      this.set_up = get_up
        // }

    onDisable(): void {
    }
}